package com.admission.service;

import java.util.List;

import org.apache.commons.lang.xwork.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;

import com.admission.dao.ArrayInfoDao;
import com.admission.model.ArrayInfo;
import com.admission.util.NumGenerator;

@Repository
@Transactional
public class ArrayInfoManager {
	private ArrayInfoDao arrayInfoDao;

	/**
	 * 通过id找到唯一
	 * 
	 * @param id
	 * @return
	 */
	public ArrayInfo getArrayInfoByid(String id) {
		return arrayInfoDao.get(id);
	}

	public ArrayInfo getByItName(String name) {
		return arrayInfoDao.findUniqueBy("itemName", name);
	}

	/**
	 * 通过大类name找到小类，分页显示
	 * 
	 * @param page
	 * @param name
	 * @return
	 */
	public Page<ArrayInfo> getByValue(final Page<ArrayInfo> page, String name) {
		Criterion criterions = Restrictions.eq("arrayInfo.id",
				getByItName(name).getId());
		return arrayInfoDao.findPage(page, criterions);

	}

	/**
	 * 批量删除
	 * 
	 * @param idstr
	 */
	public void delete(String idstr) {
		String[] ids = idstr.split(",");
		for (String id : ids) {
			arrayInfoDao.delete(id);
		}
	}

	/**
	 * 新增或修改
	 * 
	 * @param entity
	 * @param arrayId
	 */
	public void save(ArrayInfo entity, String arrayId) {
		String tNo = "";
		int tNo2 = 0;
		if (StringUtils.isEmpty(entity.getId())) {
			tNo = getMaxNo();
			tNo2 = getMaxPNo(arrayId);
			tNo = NumGenerator.getNextNum(8, tNo);
			entity.setItemName(tNo);
			entity.setOrderNo(tNo2 + 1); 
		}
		if (!StringUtils.isEmpty(arrayId)) {
			entity.setArrayInfo(getArrayInfoByid(arrayId));
		}

		arrayInfoDao.save(entity);
	}

	public int getMaxPNo(String arrayId) {
		 
			int a=	arrayInfoDao
				.findUnique("select max(a.orderNo) from ArrayInfo a where a.arrayInfo='"
						+ arrayId + "'");
				return a;
	}

	/**
	 * 查找学校概况的大分类
	 * 
	 * @return
	 */
	public List<ArrayInfo> getSchoolLife() {
		List<ArrayInfo> l = arrayInfoDao.findBy("itemName",
				ArrayInfo.SCHOOLLIFE);
		if (!l.equals(null)) {
			return l.get(0).getArrayInfos();
		}
		return null;
	}

	/**
	 * 获取省列表
	 * 
	 * @return
	 */
	public List<ArrayInfo> getProvinces() {
		List<ArrayInfo> l = arrayInfoDao.findBy("itemName", ArrayInfo.PROVINCE);
		if (!l.equals(null)) {
			return l.get(0).getArrayInfos();
		}
		return null;
	}

	/**
	 * 获得科类列表
	 * 
	 * @return
	 */
	public List<ArrayInfo> getTypes() {
		List<ArrayInfo> l = arrayInfoDao.findBy("itemName", ArrayInfo.TYPE);
		if (!l.equals(null)) {
			return l.get(0).getArrayInfos();
		}
		return null;
	}

	/**
	 * 
	 * 获取所有的数据字典
	 * 
	 * @return
	 */
	public List<ArrayInfo> getAll() {
		return arrayInfoDao.getAll();
	}

	@Autowired
	public void setArrayInfoDao(ArrayInfoDao arrayInfoDao) {
		this.arrayInfoDao = arrayInfoDao;
	}

	public String getMaxNo() {
		return arrayInfoDao
				.findUnique("select max(a.itemName) from ArrayInfo a ");
	}

	/**
	 * 改变控制状态
	 * 
	 * @param id
	 * @param status
	 */
	public void changeConsult(String status, String name) {
		ArrayInfo a = arrayInfoDao.findUniqueBy("itemName", name);
		a.setItemValue(status);
		arrayInfoDao.save(a);
	}

	/**
	 * 判断名师留言权限
	 * 
	 * @return
	 */
	public String checkConsult(String name) {
		ArrayInfo a = arrayInfoDao.findUniqueBy("itemName", name);
		return a.getItemValue();
	}

}
